package org.fisco.bcos.evidence.gateway.utils.json;

import org.fisco.bcos.evidence.gateway.utils.string.StringUtils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.google.common.collect.Maps;
import org.springframework.cglib.beans.BeanMap;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class JsonUtils {
    private static final ObjectMapper JSON = new ObjectMapper();

    static {
        JSON.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        JSON.configure(SerializationFeature.INDENT_OUTPUT, Boolean.TRUE);
    }

    /**
     * 将json字符串转换成List<Map>集合
     */
    public static List convertJson2List(String json) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        List<LinkedHashMap<String, Object>> list = null;

        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        try {
            list = objectMapper.readValue(json, List.class);
        } catch (JsonParseException e) {
            throw e;
        } catch (JsonMappingException e) {
            throw e;
        } catch (IOException e) {
            throw e;
        }
        return list;
    }

    public static <T> List<T> convertJson2List(String json, Class<T> target) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        List<T> list = null;
        try {
            JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, target);
            list = objectMapper.readValue(json, javaType);
        } catch (JsonParseException e) {
            throw e;
        } catch (JsonMappingException e) {
            throw e;
        } catch (IOException e) {
            throw e;
        }
        return list;
    }

    /**
     * 将json字符串转换为key-value结构
     *
     * @param json
     * @return
     * @throws Exception
     */
    public static Map<String, Object> convertJson2Map(String json) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> map = null;

        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        try {
            map = objectMapper.readValue(json, Map.class);
        } catch (JsonParseException e) {
            throw e;
        } catch (JsonMappingException e) {
            throw e;
        } catch (IOException e) {
            throw e;
        }
        return map;
    }

    /**
     * JavaBean(EntityData/Model)转换成JSON
     */
    public static String writeEntityJSON(Object obj) {
        ObjectMapper objectMapper = new ObjectMapper();
        String res = null;
        try {
            res = objectMapper.writeValueAsString(obj);
        } catch (IOException e) {
            return "json字符串转换失败";
        }
        return res;
    }

    /**
     * 将Entity转换成为key-value结构
     *
     * @param obj
     * @return
     * @throws IOException
     */
    public static Map<String, String> convertEntity2Map(Object obj) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        String json = null;
        Map<String, String> map = null;

        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        try {
            json = objectMapper.writeValueAsString(obj);
            map = objectMapper.readValue(json, Map.class);
        } catch (IOException e) {
            throw e;
        }
        return map;
    }

    /**
     * 将json字符串转换成为Class对应的Entity
     *
     * @param content
     * @param cls
     * @param <T>
     * @return
     * @throws IOException
     */
    public static <T> T jsonStr2Object(String content, Class cls) throws IOException {
        if (StringUtils.isBlank(content)) return null;
        ObjectMapper objectMapper = new ObjectMapper();

        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        T obj = (T) objectMapper.readValue(content, cls);
        return obj;
    }


    /**
     * 将json字符串转换成List<T>集合
     */
    public static List json2List(String json) throws Exception {
        if (json == null || json.equals(""))
            return null;
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        List<Object> list = objectMapper.readValue(json, new TypeReference<List<Object>>() {
        });
        return list;
    }

    /***
     * add by deng
     * @date 2020/01/31 15:29
     * */
    public static String toJson(Object obj) {
        try {
            return JSON.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return null;
    }


    /**
     * 将对象转化为map
     *
     * @param bean
     * @param <T>
     * @return
     */
    public static <T> Map<String, Object> beanToMap(T bean) {
        Map<String, Object> map = Maps.newHashMap();
        if (bean != null) {
            BeanMap beanMap = BeanMap.create(bean);
            for (Object key : beanMap.keySet()) {
                map.put(key.toString(), beanMap.get(key));
            }
        }
        return map;
    }

}
